package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FaqView extends Composite {

	//private int idFaq;
	
	public FaqView(int idFaq, final int user)
	{
		//this.idFaq = idFaq;
		
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("100%");
		verticalPanel.setStyleName("loginPanel");
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		verticalPanel.add(verticalPanel_1);
		//verticalPanel_1.setWidth("450");
		verticalPanel_1.setSpacing(5);

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.setWidth("100%");

		final Label verFaqLabel = new Label("Ver FAQ");
		horizontalPanel.add(verFaqLabel);
		verFaqLabel.setStyleName("lblSectionTitle");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_1);

		final Label label = new Label("Autor:");
		horizontalPanel_1.add(label);
		label.setStyleName("section_title");

		final Label lblAutor = new Label("");
		horizontalPanel_1.add(lblAutor);
		lblAutor.setStyleName("label");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_2);

		final Label dataLabel = new Label("DATA:");
		horizontalPanel_2.add(dataLabel);
		dataLabel.setStyleName("section_title");

		final Label lblData = new Label("");
		horizontalPanel_2.add(lblData);
		lblData.setStyleName("label");

		final Label label_2 = new Label("Questão:");
		verticalPanel_1.add(label_2);
		label_2.setStyleName("section_title");

		final Label lblQuestao = new Label("");
		verticalPanel_1.add(lblQuestao);
		lblQuestao.setStyleName("label");

		final Label label_3 = new Label("Resposta:");
		verticalPanel_1.add(label_3);
		label_3.setStyleName("section_title");

		final Label lblResposta = new Label("");
		verticalPanel_1.add(lblResposta);
		lblResposta.setStyleName("label");

		final Button button = new Button();
		verticalPanel_1.add(button);
		button.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				
				Default.loadComposite(new FaqList(user));
			}
		});
		button.setStyleName("buttonVoltar");
		button.setText("« Voltar");
		
		ServiceFaq.Util.getInstance().viewFaq(new Integer(idFaq),new GetAsyncCallback(lblData, lblAutor, lblQuestao, lblResposta));

	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call
	 * 
	 * This class is the implementation of AsyncCallback for the register
	 * user service
	 * 
	 * @author Amigos do Noddy
	 */	
	  static class GetAsyncCallback implements AsyncCallback {
		  Label lblData;
		  Label lblAutor;
		  Label lblQuestao;
		  Label lblResposta;
		  
		  /**
		   * Class constructor
		   * @param html Widget that can contain arbitrary HTML
		   */
		  public GetAsyncCallback(Label lblData, Label lblAutor, Label lblQuestao, Label lblResposta) {
			  this.lblData = lblData;
			  this.lblAutor = lblAutor;
			  this.lblQuestao = lblQuestao;
			  this.lblResposta = lblResposta;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {		  
			  ArrayList ret = (ArrayList)object;
			 
			  String data = ret.get(1).toString();
			  data = data.substring(0, data.indexOf(' '));
			  
			  
			  lblAutor.setText("  " + ret.get(0).toString());
			  lblData.setText("  " + data);
			  lblQuestao.setText("  " + ret.get(2).toString());
			  lblResposta.setText("  " + ret.get(3).toString());
			  
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
	}



}
